home *** CD-ROM | disk | FTP | other *** search
/ Sound Fx / Sound Fx.iso / Software / UNZIPED / MPW181-5 / _SETUP.1 / header.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-19  |  12.0 KB  |  438 lines

  1. /*    header.cpp
  2.  
  3.     Implementation of MPEG header class
  4.  
  5.     A few layer III, MPEG-2 LSF, and seeking modifications made by
  6.    Jeff Tsay. MPEG-2 LSF is not yet supported.
  7.  
  8.    Last modified : 04/19/97 */
  9.  
  10.    /*
  11.  *  @(#) header.cc 1.8, last edit: 6/15/94 16:51:44
  12.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  13.  *  @(#) Berlin University of Technology
  14.  *
  15.  *  This program is free software; you can redistribute it and/or modify
  16.  *  it under the terms of the GNU General Public License as published by
  17.  *  the Free Software Foundation; either version 2 of the License, or
  18.  *  (at your option) any later version.
  19.  *
  20.  *  This program is distributed in the hope that it will be useful,
  21.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  *  GNU General Public License for more details.
  24.  *
  25.  *  You should have received a copy of the GNU General Public License
  26.  *  along with this program; if not, write to the Free Software
  27.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  */
  29.  
  30. /*
  31.  *  Changes from version 1.1 to 1.2:
  32.  *    - iostreams manipulator calls like "cerr << setw (2) << ..." replaced by
  33.  *      "cerr.width (2); ..." due to problems with older GNU C++ releases.
  34.  *    - syncword recognition slightly changed
  35.  */
  36.  
  37.  
  38. #ifdef  __WIN32__
  39. #define STRICT
  40. #include <windows.h>
  41. #else
  42. #endif   // __WIN32__
  43.  
  44. #ifndef GUI
  45. #include <iostream.h>
  46. #endif  // GUI
  47.  
  48. #include "header.h"
  49.  
  50. const uint32 Header::frequencies[2][4] =
  51. {{22050, 24000, 16000, 1},
  52.  {44100, 48000, 32000, 1}};
  53.  
  54. Header::Header()
  55. {
  56.       framesize = 0;
  57.    nSlots    = 0;
  58.       crc       = NULL;
  59.    offset    = NULL;
  60.  
  61.    initial_sync = FALSE;
  62. }
  63.  
  64. Header::~Header()
  65. {
  66.    delete [] offset;
  67. }
  68.  
  69. BOOL Header::read_header(Ibitstream *stream, Crc16 **crcp)
  70. {
  71.   uint32 headerstring, channel_bitrate;
  72.  
  73.   if (!initial_sync) {
  74.  
  75.       if (!stream->get_header(&headerstring, INITIAL_SYNC))
  76.          return(FALSE);
  77.  
  78.       h_version = (e_version) ((headerstring >> 19) & 1);
  79.  
  80.    // temporary code, take out when MPEG-2 LSF is implemented
  81.    if (h_version == MPEG2_LSF)
  82.    {
  83. #ifdef WIN32GUI
  84.          MessageBox(NULL, "Sorry, MPEG-2 LSF is not supported yet.",
  85.                        "Stream not supported", MB_ICONSTOP | MB_OK);
  86. #else
  87.          cerr << "Sorry, MPEG-2 LSF is not supported yet." << endl;
  88. #endif
  89.         return(FALSE);
  90.    }
  91.  
  92.    if ((h_sample_frequency = (e_sample_frequency)
  93.                              ((headerstring >> 10) & 3)) == 3)
  94.    {
  95. #ifdef WIN32GUI
  96.          MessageBox(NULL, "Unknown sample frequency in header",
  97.                         "Stream not supported", MB_ICONSTOP | MB_OK);
  98. #else
  99.          cerr << "Unknown sample frequency!" << endl;
  100. #endif
  101.         return(FALSE);
  102.    }
  103.  
  104.    stream->set_syncword(headerstring & 0xFFF80CC0);
  105.  
  106.    initial_sync = TRUE;
  107.  
  108.   } else {
  109.  
  110.           if (!stream->get_header(&headerstring, STRICT_SYNC))
  111.              return(FALSE);
  112.   } // initial_sync
  113.  
  114. /*  if ((h_layer = (headerstring >> 17) & 3) == 0)
  115.   {
  116.      cerr << "unknown layer identifier found!\n";
  117.      exit (1);
  118.   }
  119.   h_layer = 4 - h_layer;        // now 1 means Layer I and 3 means Layer III   */
  120.  
  121.   h_layer   = 4 - (headerstring >> 17) & 3;
  122.  
  123.   h_protection_bit = (headerstring >> 16) & 1;
  124.  
  125.  
  126. /*  if ((h_bitrate_index = (headerstring >> 12) & 0xF) == 15)
  127.   {
  128.      cerr << "unknown bitrate index found!\n";
  129.      exit (1);
  130.   }           */
  131. /*  if (!h_bitrate_index)
  132.   {
  133.      cerr << "free format not yet implemented!\n";
  134.      exit (1);
  135.   } */
  136.  
  137.   h_bitrate_index  = (headerstring >> 12) & 0xF;
  138.  
  139.   h_padding_bit = (headerstring >> 9) & 1;
  140.   h_mode        = (e_mode)((headerstring >> 6) & 3);
  141.  
  142. /*  if (h_layer == 2)
  143.      // testing validity of mode and bitrate:
  144.      if ((((h_bitrate_index >= 1 && h_bitrate_index <= 3) || h_bitrate_index == 5) &&
  145.      h_mode != single_channel) ||
  146.     (h_bitrate_index >= 11 && h_mode == single_channel))
  147.      {
  148.         cerr << "illegal combination of mode and bitrate in a layer II stream:\n"
  149.             "  mode: " << mode_string ()
  150.         << "\n  bitrate: " << bitrate_string () << '\n';
  151.         exit (1);
  152.      } */
  153.  
  154.   h_mode_extension = (headerstring >> 4) & 3;
  155.  
  156.   if (h_mode == joint_stereo)
  157.      h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
  158.   else
  159.      h_intensity_stereo_bound = 0;        // should never be used
  160.  
  161.   h_copyright = (BOOL) ((headerstring >> 3) & 1);
  162.   h_original  = (BOOL) ((headerstring >> 2) & 1);
  163.  
  164.   // calculate number of subbands:
  165.   if (h_layer == 1)
  166.      h_number_of_subbands = 32;
  167.   else
  168.   {
  169.      channel_bitrate = h_bitrate_index;
  170.  
  171.      // calculate bitrate per channel:
  172.      if (h_mode != single_channel)
  173.         if (channel_bitrate == 4)
  174.             channel_bitrate = 1;
  175.         else
  176.             channel_bitrate -= 4;
  177.  
  178.      if ((channel_bitrate == 1) || (channel_bitrate == 2))
  179.         if (h_sample_frequency == thirtytwo)
  180.             h_number_of_subbands = 12;
  181.         else
  182.             h_number_of_subbands = 8;
  183.      else
  184.         if ((h_sample_frequency == fourtyeight) || ((channel_bitrate >= 3) &&
  185.                                                                   (channel_bitrate <= 5)))
  186.             h_number_of_subbands = 27;
  187.         else
  188.             h_number_of_subbands = 30;
  189.   }
  190.  
  191.   if (h_intensity_stereo_bound > h_number_of_subbands)
  192.      h_intensity_stereo_bound = h_number_of_subbands;
  193.  
  194.  
  195.   // calculate framesize and nSlots
  196.     calculate_framesize();
  197.  
  198.   // read framedata:
  199.   if (stream->read_frame(framesize) == FALSE)
  200.      return(FALSE);
  201.  
  202.   if (!h_protection_bit)
  203.   {
  204.      // frame contains a crc checksum
  205.      checksum = (uint16) stream->get_bits(16);
  206.      if (!crc)
  207.        crc = new Crc16;
  208.      crc->add_bits(headerstring, 16);
  209.      *crcp = crc;
  210.   }
  211.   else
  212.      *crcp = NULL;
  213.  
  214.   if (h_sample_frequency == fourtyfour_point_one) {
  215.  
  216.      if (!offset) {
  217.  
  218.         uint32 max = max_number_of_frames(stream);
  219.  
  220.         offset = new uint32[max];
  221.  
  222.       for(uint32 i=0; i<max; i++)
  223.           offset[i] = 0;
  224.     }
  225.  
  226.     {
  227.      int32 cf = stream->current_frame();
  228.      int32 lf = stream->last_frame();
  229.       if ((cf > 0) && (cf == lf)) {
  230.            offset[cf] = offset[cf-1] + h_padding_bit;
  231.      } else {
  232.             offset[0] = h_padding_bit;
  233.      }
  234.     }
  235.   }
  236.  
  237.   return(TRUE);
  238. }
  239.  
  240.  
  241. uint32 Header::calculate_framesize()
  242. // calculates framesize in bytes excluding header size
  243. {
  244.   static const int32 bitrates[2][3][16] = {
  245.   {{0 /*free format*/, 32000, 48000, 56000, 64000, 80000, 96000,
  246.     112000, 128000, 144000, 160000, 176000, 192000 ,224000, 256000, 0},
  247.    {0 /*free format*/, 8000, 16000, 24000, 32000, 40000, 48000,
  248.     56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 0},
  249.    {0 /*free format*/, 8000, 16000, 24000, 32000, 40000, 48000,
  250.     56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 0}},
  251.   {{0 /*free format*/, 32000, 64000, 96000, 128000, 160000, 192000,
  252.      224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000, 0},
  253.     {0 /*free format*/, 32000, 48000, 56000, 64000, 80000, 96000,
  254.      112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000, 0},
  255.     {0 /*free format*/, 32000, 40000, 48000, 56000, 64000, 80000,
  256.      96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 0}}
  257.   };
  258.  
  259.   if (h_layer == 1)
  260.   {
  261.      framesize = (12 * bitrates[h_version][0][h_bitrate_index]) /
  262.                  frequencies[h_version][h_sample_frequency];
  263.  
  264.      if (h_padding_bit) framesize++;
  265.  
  266.      framesize <<= 2;        // one slot is 4 bytes long
  267.   }
  268.   else
  269.   {
  270.      framesize = (144 * bitrates[h_version][h_layer - 1][h_bitrate_index]) /
  271.                  frequencies[h_version][h_sample_frequency];
  272.  
  273.      if (h_version == MPEG2_LSF)
  274.         framesize >>= 1;
  275.  
  276.      if (h_padding_bit) framesize++;
  277.  
  278.      // Layer III slots
  279.      if (h_layer == 3) {
  280.  
  281.        if (h_version == MPEG1) {
  282.  
  283.              nSlots = framesize - ((h_mode == single_channel) ? 17 : 32) // side info size
  284.                                       -  (h_protection_bit ? 0 : 2)                // CRC size
  285.                                       - 4;                                              // header size
  286.        } else {  // MPEG-2 LSF
  287.              nSlots = framesize - ((h_mode == single_channel) ?  9 : 17) // side info size
  288.                                      -  (h_protection_bit ? 0 : 2)                // CRC size
  289.                                       - 4;                                              // header size
  290.        }
  291.      } else {
  292.          nSlots = 0;
  293.     }
  294.   }
  295.  
  296.   framesize -= 4;             // subtract header size
  297.  
  298.   return(framesize);
  299. }
  300.  
  301. const char *Header::layer_string()
  302. {
  303.   switch (h_layer)
  304.   {
  305.      case 1:
  306.         return "I                                      ";
  307.      case 2:
  308.         return "II                                     ";
  309.      case 3:
  310.         return "III                                    ";
  311.   }
  312.   return NULL;            // dummy
  313. }
  314.  
  315.  
  316. const char *Header::bitrate_string()
  317. {
  318.  
  319.   static const char *bitrate_str[2][3][16] = {
  320.   {{"free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s",
  321.     "80 kbit/s", "96 kbit/s", "112 kbit/s", "128 kbit/s", "144 kbit/s",
  322.     "160 kbit/s", "176 kbit/s", "192 kbit/s", "224 kbit/s", "256 kbit/s",
  323.     "forbidden"},
  324.    {"free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s",
  325.     "40 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
  326.     "96 kbit/s", "112 kbit/s", "128 kbit/s", "144 kbit/s", "160 kbit/s",
  327.     "forbidden"},
  328.    {"free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s",
  329.     "40 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
  330.     "96 kbit/s", "112 kbit/s", "128 kbit/s", "144 kbit/s", "160 kbit/s",
  331.     "forbidden"}},
  332.   {{"free format", "32 kbit/s", "64 kbit/s", "96 kbit/s", "128 kbit/s",
  333.     "160 kbit/s", "192 kbit/s", "224 kbit/s", "256 kbit/s", "288 kbit/s",
  334.     "320 kbit/s", "352 kbit/s", "384 kbit/s", "416 kbit/s", "448 kbit/s",
  335.     "forbidden"},
  336.     {"free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s",
  337.     "80 kbit/s", "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s",
  338.     "192 kbit/s", "224 kbit/s", "256 kbit/s", "320 kbit/s", "384 kbit/s",
  339.     "forbidden"},
  340.     {"free format", "32 kbit/s", "40 kbit/s", "48 kbit/s", "56 kbit/s",
  341.     "64 kbit/s", "80 kbit/s" , "96 kbit/s", "112 kbit/s", "128 kbit/s",
  342.     "160 kbit/s", "192 kbit/s", "224 kbit/s", "256 kbit/s", "320 kbit/s",
  343.     "forbidden"}}
  344.   };
  345.  
  346.   return(bitrate_str[h_version][h_layer - 1][h_bitrate_index]);
  347. }
  348.  
  349. const char *Header::sample_frequency_string()
  350. {
  351.   switch (h_sample_frequency)
  352.   {
  353.      case thirtytwo:
  354.         if (h_version == MPEG1)
  355.             return("32 kHz        ");
  356.       else
  357.           return("16 kHz        ");
  358.      case fourtyfour_point_one:
  359.         if (h_version == MPEG1)
  360.             return("44.1 kHz     ");
  361.       else
  362.           return("22.05 kHz   ");
  363.      case fourtyeight:
  364.         if (h_version == MPEG1)
  365.             return("48 kHz        ");
  366.       else
  367.           return("24 kHz        ");
  368.   }
  369.   return(NULL);            // dummy
  370. }
  371.  
  372. const char *Header::mode_string()
  373. {
  374.   switch (h_mode)
  375.   {
  376.      case stereo:
  377.         return("Stereo                             ");
  378.      case joint_stereo:
  379.         return("Joint stereo                      ");
  380.      case dual_channel:
  381.         return("Dual channel                    ");
  382.      case single_channel:
  383.         return("Single channel                ");
  384.   }
  385.   return(NULL);            // dummy
  386. }
  387.  
  388. const char *Header::version_string()
  389. {
  390.      switch (h_version)
  391.    {
  392.        case MPEG1:
  393.         return("MPEG-1                    ");
  394.       case MPEG2_LSF:
  395.         return("MPEG-2 LSF                ");
  396.    }
  397.    return(NULL);
  398. }
  399.  
  400. #ifdef SEEK_STOP
  401. // Stream searching routines
  402. BOOL Header::stream_seek(Ibitstream *stream, uint32 seek_pos)
  403. {
  404.  
  405.    return ((h_sample_frequency != fourtyfour_point_one) ?
  406.              stream->seek(seek_pos, framesize) :
  407.            stream->seek_pad(seek_pos, framesize - h_padding_bit,
  408.                             this, offset));
  409. }
  410. #endif
  411.  
  412. uint32 Header::max_number_of_frames(Ibitstream *stream)
  413. // Returns the maximum number of frames in the stream
  414. {
  415.       return(stream->file_size() / (framesize + 4 - h_padding_bit));
  416. }
  417.  
  418. uint32 Header::min_number_of_frames(Ibitstream *stream)
  419. // Returns the minimum number of frames in the stream
  420. {
  421.       return(stream->file_size() / (framesize + 5 - h_padding_bit));
  422. }
  423.  
  424. real Header::ms_per_frame()
  425. {
  426.     static real ms_per_frame_array[3][3] = {{8.707483f,  8.0f, 12.0f},
  427.                                                        {26.12245f, 24.0f, 36.0f},
  428.                                             {26.12245f, 24.0f, 36.0f}};
  429.  
  430.     return(ms_per_frame_array[h_layer-1][h_sample_frequency]);
  431. }
  432.  
  433. real Header::total_ms(Ibitstream *stream)
  434. {
  435.     return(max_number_of_frames(stream) * ms_per_frame());
  436. }
  437.  
  438.